package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.zsyh.domain.ZsyhAfterFactoryLogin;
import com.ruoyi.zsyh.service.IZsyhAfterFactoryLoginService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhAfterFactory;
import com.ruoyi.zsyh.service.IZsyhAfterFactoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 售后厂家Controller
 * 
 * @author yuexl
 * @date 2022-08-22
 */
@Controller
@RequestMapping("/zsyh/afterFactory")
public class ZsyhAfterFactoryController extends BaseController
{
    private String prefix = "zsyh/afterFactory";

    @Autowired
    private IZsyhAfterFactoryService zsyhAfterFactoryService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IZsyhAfterFactoryLoginService zsyhAfterFactoryLoginService;

    @RequiresPermissions("zsyh:afterFactory:view")
    @GetMapping()
    public String afterFactory()
    {
        return prefix + "/afterFactory";
    }

    /**
     * 查询售后厂家列表
     */
    @RequiresPermissions("zsyh:afterFactory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhAfterFactory zsyhAfterFactory)
    {
        startPage();
        zsyhAfterFactory.setIsValid("1");
        List<ZsyhAfterFactory> list = zsyhAfterFactoryService.selectZsyhAfterFactoryList(zsyhAfterFactory);
        return getDataTable(list);
    }

    /**
     * 导出售后厂家列表
     */
    @RequiresPermissions("zsyh:afterFactory:export")
    @Log(title = "售后厂家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAfterFactory zsyhAfterFactory)
    {
        List<ZsyhAfterFactory> list = zsyhAfterFactoryService.selectZsyhAfterFactoryList(zsyhAfterFactory);
        ExcelUtil<ZsyhAfterFactory> util = new ExcelUtil<ZsyhAfterFactory>(ZsyhAfterFactory.class);
        return util.exportExcel(list, "售后厂家数据");
    }

    /**
     * 新增售后厂家
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存售后厂家
     */
    @RequiresPermissions("zsyh:afterFactory:add")
    @Log(title = "售后厂家", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAfterFactory zsyhAfterFactory)
    {
        String factoryId = IdWorker.getId();
        zsyhAfterFactory.setId(factoryId);
        int num = zsyhAfterFactoryService.insertZsyhAfterFactory(zsyhAfterFactory);
        if(num>0){
            //自动添加一名厂家主账号
            ZsyhAfterFactoryLogin zsyhAfterFactoryLogin = new ZsyhAfterFactoryLogin();
            zsyhAfterFactoryLogin.setId(IdWorker.getId());
            zsyhAfterFactoryLogin.setFactoryId(factoryId);
            zsyhAfterFactoryLogin.setLoginPassword(new Md5Hash(configService.selectConfigByKey("sys.user.initPassword")).toHex());
            zsyhAfterFactoryLogin.setLoginAccount(zsyhAfterFactory.getFactoryPhone());
            zsyhAfterFactoryLogin.setLoginPhone(zsyhAfterFactory.getFactoryPhone());
            zsyhAfterFactoryLogin.setLoginName(zsyhAfterFactory.getFactoryPerson());
            zsyhAfterFactoryLogin.setLoginType("1");
            zsyhAfterFactoryLogin.setLoginValid("1");
            zsyhAfterFactoryLoginService.insertZsyhAfterFactoryLogin(zsyhAfterFactoryLogin);
        }
        return toAjax(num);
    }

    /**
     * 修改售后厂家
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAfterFactory zsyhAfterFactory = zsyhAfterFactoryService.selectZsyhAfterFactoryById(id);
        mmap.put("zsyhAfterFactory", zsyhAfterFactory);
        return prefix + "/edit";
    }

    /**
     * 修改保存售后厂家
     */
    @RequiresPermissions("zsyh:afterFactory:edit")
    @Log(title = "售后厂家", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAfterFactory zsyhAfterFactory)
    {
        return toAjax(zsyhAfterFactoryService.updateZsyhAfterFactory(zsyhAfterFactory));
    }

    /**
     * 删除售后厂家
     */
    @RequiresPermissions("zsyh:afterFactory:remove")
    @Log(title = "售后厂家", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //修改厂家有效状态为失效
        ZsyhAfterFactory zsyhAfterFactory = new ZsyhAfterFactory();
        zsyhAfterFactory.setId(ids);
        zsyhAfterFactory.setIsValid("0");
        return toAjax(zsyhAfterFactoryService.updateZsyhAfterFactory(zsyhAfterFactory));
    }
}
